package h0910;

/**
 * @author 不知名帅哥
 * @version 1.0
 * @description 长度最小的子数组
 * @date 2024/9/10 19:49
 */
public class SmallestSubArray {
    public static int minSubArrayLen(int target, int[] nums) {
        //滑动窗口
        int len = Integer.MAX_VALUE;
        int start = 0;
        int sum = 0;
        for (int end = 0; end < nums.length; end++) {
            sum += nums[end];
            //调节窗口
            while (sum >= target) {
                len = Math.min(len, end - start + 1);
                sum -= nums[start];
                start++;
            }
        }
        return len != Integer.MAX_VALUE ? len : 0;
    }

    public static void main(String[] args) {
        int[] nums = {1, 2, 3, 4, 5};
        int target = 11;
        System.out.println(minSubArrayLen(target, nums));
    }
}
